package my.mall.front.api.controller

import jakarta.validation.Valid
import my.mall.common.enums.RespMetaEnum
import my.mall.common.pojo.CommonResp
import my.mall.common.util.RespUtils.warning
import my.mall.front.api.service.UserService
import my.mall.front.api.pojo.user.UserParam
import my.mall.front.api.service.sms.SmsService
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/user")
class UserController(
    private val userService: UserService,
    private val smsService: SmsService
) {

    @PostMapping("/sendMsg")
    fun sendMsg(@RequestBody userParam: @Valid UserParam): CommonResp<*> {
        return smsService.sendMsg(userParam.phoneNum)
    }

    @PostMapping("/login")
    fun login(@RequestBody userParam: @Valid UserParam): CommonResp<*> {
        return if (userParam.code == null || userParam.code!! < 100000 || userParam.code!! > 999999) {
            warning<Any>(RespMetaEnum.PARAM_ERROR)
        } else userService.login(userParam.phoneNum, userParam.code!!)
    }
}
